package com.hellogood.work.evaluate.client.service;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.work.evaluate.client.common.CommonMessages;
import com.hellogood.work.evaluate.shared.po.Service;
import com.hellogood.work.evaluate.shared.services.ServiceService;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.IntegerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;

public class ModifyWindow extends Window {
	private ServiceTab tab = null;
	private boolean isModify = false;
	private String usercode = null;
	private String username = null;
	private String score = null;
	private TextItem codeItem = null;
	private TextItem nameItem = null;
	private IntegerItem scoreItem = null;
	private Button saveBtn = null;
	private Label msg = null;
	private DynamicForm form = null;

	public ModifyWindow(ServiceTab tab, String usercode, String username, String score,boolean isModify) {
		this.tab = tab;
		this.isModify = isModify;
		this.usercode = usercode;
		this.username = username;
		this.score = score;
		initView();
	}

	private void initView() {
		LengthRangeValidator length5Validator = new LengthRangeValidator();
		length5Validator.setMin(1);
		length5Validator.setMax(5);
		length5Validator.setErrorMessage(CommonMessages.MaxLength5);

		codeItem = new TextItem("code", "服务编号");
		codeItem.setCanEdit(false);
		codeItem.setLength(4);
		nameItem = new TextItem("name", "服务名称");
		nameItem.setRequired(true);
		nameItem.setLength(10);
		nameItem.setValidators(length5Validator);
		scoreItem = new IntegerItem("score", "服务分值");
		scoreItem.setRequired(true);
		if (isModify) {
			codeItem.setValue(usercode);
			nameItem.setValue(username);
			scoreItem.setValue(score);
			setTitle("修改服务");
		} else {
			codeItem.setRequired(true);
			codeItem.setCanEdit(true);
			setTitle("添加服务");
		}

		nameItem.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName() != null && event.getKeyName().equals("Enter")) {
					saveBtn.focus();
				}
			}
		});

		form = new DynamicForm();
		form.setSize("300", "80");
		form.setFields(codeItem, nameItem, scoreItem);

		saveBtn = new Button("保存");
		saveBtn.addClickHandler(new SaveBtnClickHandler());

		msg = new Label();
		msg.setSize("350", "30");

		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(saveBtn);
		layout.addMember(msg);
		layout.setTop(50);
		layout.setLeft(15);
		addChild(layout);
	}

	public TextItem getTextItem() {
		return codeItem;
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				Object code = codeItem.getValue();
				Object name = nameItem.getValue();
				Object score = scoreItem.getValue();
				if (code != null && name != null && score != null) {
					Service s = new Service();
					s.setServiceCode(code.toString().trim());
					s.setServiceName(name.toString().trim());
					s.setScore(Integer.valueOf(score.toString()));
					ServiceService.Util.getInstance().modify(s, ModifyWindow.this.isModify, new ModifyAsyncCallback());
				} else {
					msg.setContents(CommonMessages.CodeNameInputError);
				}
			}
		}

	}

	private class ModifyAsyncCallback implements AsyncCallback<String> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(String result) {
			if (result.equals("")) {
				msg.setContents(CommonMessages.SaveSuccess);
				ModifyWindow.this.tab.initData();
				if (isModify) {
					destroy();
				} else {
					codeItem.setValue("");
					nameItem.setValue("");
					scoreItem.setValue("");
					saveBtn.setTitle(CommonMessages.CreateAgain);
				}
			} else {
				msg.setContents(CommonMessages.SaveFailed + " " + result);
			}
		}
	}

}
